﻿package com.kuanter.kuanterauto.alipay;


import java.net.URLEncoder;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.kuanter.kuanterauto.R;

public class AlipayCenter {
	private Context context ;
	private ProgressDialog mProgress = null;
	
	public AlipayCenter(Context context) {
		this.context = context;
	}

	public boolean payPrepareForcheckInstallAlipay(){
		boolean isPrepare = false ;
		// 检测安全支付服务是否安装
				MobileSecurePayHelper mspHelper = new MobileSecurePayHelper(context);
				boolean isMobile_spExist = mspHelper.detectMobile_sp();
				if (!isMobile_spExist){
					isPrepare = false;
					return isPrepare;
				}
				// check some info.
				// 检测配置信息
				if (!checkInfoForPartnerAndSeller()) {
					BaseHelper.showDialog(
									(Activity)context,
									"提示",
									"缺少partner或者seller，请在src/com/alipay/android/appDemo4/PartnerConfig.java中增加。",
									R.drawable.infoicon);
					isPrepare = false ;
					return isPrepare;
				}else 
					isPrepare = true ;
				return isPrepare ;
	}
	
	public boolean checkInfoForPartnerAndSeller() {
		String partner = PartnerConfig.PARTNER;
		String seller = PartnerConfig.SELLER;
		if (partner == null || partner.length() <= 0 || seller == null
				|| seller.length() <= 0)
			return false;

		return true;
	}
	
	/**
	 * get the sign type we use. 获取签名方式
	 * 
	 * @return
	 */
	String getSignType() {
		String getSignType = "sign_type=" + "\"" + "RSA" + "\"";
		return getSignType;
	}
	
	/**
	 * sign the order info. 对订单信息进行签名
	 * 
	 * @param signType
	 *            签名方式
	 * @param content
	 *            待签名订单信息
	 * @return
	 */
	String sign(String signType, String content) {
		return Rsa.sign(content, PartnerConfig.RSA_PRIVATE);
	}
	
	public boolean payStart(String orderInfo,Handler mHandler){
		// 根据订单信息开始进行支付
				boolean bRet = false;
				try {
					// prepare the order info.
					// 准备订单信息
					//String orderInfo = getOrderInfo(position);
					// 这里根据签名方式对订单信息进行签名
					String signType = getSignType();
					String strsign = sign(signType, orderInfo);
					Log.v("sign:", strsign);
					// 对签名进行编码
					strsign = URLEncoder.encode(strsign);
					// 组装好参数
					String info = orderInfo + "&sign=" + "\"" + strsign + "\"" + "&"
							+ getSignType();
					Log.v("orderInfo:", info);
					// start the pay.
					// 调用pay方法进行支付
					MobileSecurePayer msp = new MobileSecurePayer();
					bRet = msp.pay(info, mHandler, AlixId.RQF_PAY, (Activity)context);

					if (bRet) {
						// show the progress bar to indicate that we have started
						// paying.
						// 显示“正在支付”进度条
						closeProgress();
						/*mProgress = BaseHelper.showProgress(context, null, "正在支付", false,
								true);*/
					} else
						;
				} catch (Exception ex) {
					Toast.makeText(context, R.string.remote_call_failed,
							Toast.LENGTH_SHORT).show();
				}
				return bRet;
				
	}
	// 关闭进度框
	public void closeProgress() {
		try {
			if (mProgress != null) {
				mProgress.dismiss();
				mProgress = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}

